package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.Gps.Gps;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfo;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanGps;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfoHis;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfo;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfoHis;
import com.supcon.exchangedata.repository.slave6.SlaveCarGpsInfoOrigin;
import com.supcon.exchangedata.utils.PositionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/23/16:07
 * @Description:
 */
@Service
@Slf4j
public class GarbageCarGpsService {
    @Resource
    private SlaveCarGpsInfoOrigin slaveCarGpsInfoOrigin;
    @Resource
    private SlaveCarGpsInfo slaveCarGpsInfo;
    @Resource
    private SlaveCarGpsInfoHis slaveCarGpsInfoHis;

    public void syncCleanCarGps() {
        List<CtGpsStatisitcInfo> list = new ArrayList<>();
        List<HjCleanGps> all = slaveCarGpsInfoOrigin.findCurrentBusCarStation();
        if (!CollectionUtils.isEmpty(all)) {
            try{
                slaveCarGpsInfo.deleteByTypeAndLayerCodeDiy(4L, "4012");
            }catch (Exception e){

            }

            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (HjCleanGps hjCleanGps : all) {
                LocalDateTime bizTime = hjCleanGps.getBizTime();
                String times = timeFormatter.format(bizTime);
                CtGpsStatisitcInfo gpsStatisitcInfo = new CtGpsStatisitcInfo();
                String carNumber = hjCleanGps.getCarNumber();
                    gpsStatisitcInfo.setLicensePlateNum(carNumber);
                    gpsStatisitcInfo.setType(4L);
                    gpsStatisitcInfo.setCurrTime(times);
                    gpsStatisitcInfo.setLayerCode("4012");
                    if (!StringUtils.isEmpty(hjCleanGps.getSpeed())) {
                        try {
                            gpsStatisitcInfo.setSpeed(new BigDecimal(hjCleanGps.getSpeed()));
                        }catch (Exception e){
                            System.out.println("速度:"+hjCleanGps.getSpeed());
                        }

                    }
                    String oriLatitude = hjCleanGps.getOriLatitude();
                    String oriLongitude = hjCleanGps.getOriLongitude();
                    if (!StringUtils.isEmpty(oriLatitude) && !StringUtils.isEmpty(oriLongitude)) {
                        Gps gps = PositionUtil.gcj_To_Gps84(Double.valueOf(oriLongitude), Double.valueOf(oriLatitude));
                        gpsStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                        gpsStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                        list.add(gpsStatisitcInfo);
                }
            }
        }

        if(!CollectionUtils.isEmpty(list)){
            slaveCarGpsInfo.saveAll(list);
        }
    }

    public void  newSyncCleanCarGps(){
        List<CtGpsStatisitcInfoHis> list = new ArrayList<>();
        Integer aLong = slaveCarGpsInfoOrigin.maxTongId().getTongid();
        if (aLong!=null){
            CtGpsStatisitcInfoHis one = slaveCarGpsInfoHis.findByTongId(aLong.toString());
            if (one==null){
                List<HjCleanGps> all = slaveCarGpsInfoOrigin.findAll();
                if (!CollectionUtils.isEmpty(all)) {
                    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    for (HjCleanGps hjCleanGps : all) {
                        LocalDateTime bizTime = hjCleanGps.getBizTime();
                        String times = timeFormatter.format(bizTime);
                        CtGpsStatisitcInfoHis gpsStatisitcInfo = new CtGpsStatisitcInfoHis();
                        String carNumber = hjCleanGps.getCarNumber();
                        gpsStatisitcInfo.setLicensePlateNum(carNumber);
                        gpsStatisitcInfo.setType(4L);
                        gpsStatisitcInfo.setCurrTime(times);
                        gpsStatisitcInfo.setLayerCode("4012");
                        gpsStatisitcInfo.setTongId(hjCleanGps.getTongid().toString());
                        if (!StringUtils.isEmpty(hjCleanGps.getSpeed())) {
                            try {
                                gpsStatisitcInfo.setSpeed(new BigDecimal(hjCleanGps.getSpeed()));
                            }catch (Exception e){
                                System.out.println("速度:"+hjCleanGps.getSpeed());
                            }

                        }
                        String oriLatitude = hjCleanGps.getOriLatitude();
                        String oriLongitude = hjCleanGps.getOriLongitude();
                        if (!StringUtils.isEmpty(oriLatitude) && !StringUtils.isEmpty(oriLongitude)) {
                            Gps gps = PositionUtil.gcj_To_Gps84(Double.valueOf(oriLongitude), Double.valueOf(oriLatitude));
                            gpsStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                            gpsStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                            list.add(gpsStatisitcInfo);
                        }
                    }
                }
                if(!CollectionUtils.isEmpty(list)){
                    slaveCarGpsInfoHis.saveAll(list);
                }
            }
        }
    }

}
